<?php
##############################################
#
#   活动管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\AdvertModel;


class AdvertController extends AdminBaseController {
	
	protected $_advert;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _advert  = new AdvertModel();
	}
	
	/**
	 * 广告位列表
	 */
	public function advertPosition() {
	    $data = $this->_advert->getAdvertPosition(I('name'));
	    $this->assign('advert', $data[0]);
	    $this->assign('page', $data[1]);
	    $this->display('advert_index');
	}

	/**
	 * 添加广告位
	 */
	public function addAdvertPosition() {
	    if(IS_GET){
	        $id = I('id');
	        $act = I('get.act');
	        if($id && $act == 'edit'){
	            $data = $this->_advert->getAdvertPositionFind($id);
	            $data['describe'] = htmlspecialchars_decode($data['describe']);
	            $this->assign('id', $id);
	            $this->assign('advert', $data);
	        }
	        $this->assign('act', $act); 
	        $this->display('add_advert');
	    }else if(IS_POST){
	        $data = I('post.');
	        $data['add_time'] = time();
	        $data['describe'] = htmlspecialchars($data['describe']);
	        $admin_base_model = new AdminBaseModel('advert_position');
	        $act = I('post.act');
	        if($act == 'add'){
	            $result = $admin_base_model->addData($data);
	            if($result)
	                $this->success('添加成功', U('advertPosition'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }elseif($act == 'edit'){
	            $map['id'] = I('id');
	            if($admin_base_model->editData($map, $data) !== false)
	                $this->success('修改成功', U('advertPosition'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }
	    }
	}
	
	/**
	 * 删除广告位
	 */
	public function deleteAdvertPosition() {
	    if(!IS_AJAX) return;
	    $advert_data = M('advert')->where("id = {$_GET['id']}")->count();
	    if($advert_data){
	        $this->ajaxReturn(array('status' => 2,'message' => '该广告位下有广告，请先删除广告'));
	    }
		$result = $this->_activity->deleteAdvertPosition(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	
	/**
	 * 操作
	 */
	public function operate_advert_position(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.advert_position_id');
	    if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $advert_position = M('advert_position');
	    switch ($operate){
	        case 'del':
	            $advert_data = M('advert')->where($map)->count();
	            if($advert_data){
	                $this->error('该广告位下有广告，请先删除广告', '', 2);
	            }
	            $row = $advert_position->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('is_open'=>1);
	            $row = $advert_position->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('is_open'=>2);
	            $row = $advert_position->where($map)->save($data);
	            break;
	    }
	    if($row !== false)
	        $this->success('操作成功', U('advertPosition'), 1);
	    else
	        $this->error('操作失败');
	     
	}
	
	/**
	 * 广告列表
	 */
	public function advertList() {
	    $data = $this->_advert->getAdvert(I('name'), I('positon_id'), I('media_type'));
	    foreach ($data[0] as $k => $v){
	        $data[0][$k]['position_id'] = $this->_advert->getAdvertPositionName($v['position_id']);
	    }
	    $position = $this->_advert->getPositionField();
	    $this->assign('position', $position);
	    $this->assign('advert', $data[0]);
	    $this->assign('page', $data[1]);
	    $this->display('index');
	}
	
	/**
	 * 添加广告
	 */
	public function addAdvert() {
	    if(IS_GET){
	        $id = I('id');
	        $act = I('get.act');
	         if($id && $act == 'edit'){
	            $data = $this->_advert->getAdvertFind($id);
	            $data['start_time'] = date('Y-m-d H:i:s');
	            $data['end_time'] = date('Y-m-d H:i:s');
	            $this->assign('id', $id);
	            $this->assign('advert', $data);
	        }
	        $position = $this->_advert->getPositionField();
	        $this->assign('position', $position);
	        $this->assign('act', $act);
	        $this->display('add');
	    }else if(IS_POST){
	        $data = I('post.');
	        if($data['media_type'] == '1'){
    	        if($_FILES['advert_img']['name']){
    	            $photo_img = AdminBaseModel::upload(900, 255);
    	            $data['advert_img'] = $photo_img['advert_img'];  //serialize($photo_img)
    	            if($data['old_advert_img']){
    	                @unlink($_SERVER['DOCUMENT_ROOT'].substr(__APP__, 1).$data['old_advert_img']);
    	            }
    	        }else{
    	            if(!$data['old_advert_img']){
    	                $this->error('请上传广告图片', '', 1);
    	            }
    	        }
	        }
	        $data['start_time'] = strtotime($data['start_time']);
	        $data['end_time'] = strtotime($data['end_time']);
	        $data['add_time'] = time();
	        $admin_base_model = new AdminBaseModel('advert');
	        $act = I('post.act');
	        if($act == 'add'){
	            $result = $admin_base_model->addData($data);
	            if($result)
	                $this->success('添加成功', U('advertList'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }elseif($act == 'edit'){
	            $map['id'] = I('id');
	            if($admin_base_model->editData($map, $data) !== false)
	                $this->success('修改成功', U('advertList'), 1);
	            else
	                $this->error('操作失败', '', 1);
	        }
	    }
	}
	
	/**
	 * 操作
	 */
	public function operate_advert(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.advert_id');
	    if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $advert = M('advert');
	    switch ($operate){
	        case 'del':
	            $row = $advert->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('enabled'=>1);
	            $row = $advert->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('enabled'=>2);
	            $row = $advert->where($map)->save($data);
	            break;
	    }
	    if($row !== false)
	        $this->success('操作成功', U('advertList'), 1);
	    else
	        $this->error('操作失败');
	
	}
}



	
	